package ca.etsmtl.log320;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


/**************************************************************************************
 ** Code emprunt� du laboratoire 1 du cours LOG320:
 ** et modifi�
 *************************************************************************************** 
 ** Class name: FileReaderFilter
 ** Original author: A.J. Lattanze, CMU Date: 12/3/99
 ** Version 1.0
 ** 
 ** Adapted by R. Champagne, Ecole de technologie superieure 2002-May-08,
 ** 2011-Jan-12, 2012-Jan-13.
 ** 
 *************************************************************************************** 
 ** Purpose: Assignment 1 for LOG430, Architecture logicielle. This assignment is
 ** designed to illustrate a pipe and filter architecture. For the instructions,
 ** refer to the assignment write-up.
 ** 
 ** Abstract: This class reads the input file and sends its content downstream via its
 **           output pipe.
 ** 
 ** Pseudo Code:
 ** 
 ** open input file whos name is provided in constructor 
 ** 
 ** while not eof (input file)
 **    read line of text
 **    write line of text to output pipe 
 ** end while
 ** 
 ** close pipes and file
 ** 
 ** Modification Log
 ************************************************************************************** 
 ** 
 **************************************************************************************/


public class FileReader {

	// Create local pipe to that will connect to downstream filter
	File inputFile;
	public FileReader(String fileName) {
		// Lets make sure that file name is provided

		if (fileName == null) {
			//TODO: throw exception
		} else {

			// Declarations:
			inputFile = new File(fileName);
			
			// Check to ensure that the file exists
			if (!inputFile.exists()) {
				System.out.println("\nFileReader:: file  \t"
						+ inputFile.getAbsolutePath() + " does not exist.");
			} else {
//				System.out.println("FileReader:: opening file  \t"
//						+ inputFile.getAbsolutePath());
			}
		}
	}
	

	public Character[][] read() {
		char buffer[] = null;
		BufferedReader InFile = null;

        Character[][] values = new Character[9][9];
        
		try {
			// Create a buffered reader the file
			InFile = new BufferedReader(
					new InputStreamReader(new FileInputStream(
							(inputFile))));


			String LineOfText;
			boolean Done = false;
			
			int i = 0;
			while (!Done) {
				LineOfText = InFile.readLine();
				if(i == 9)
					break;
				
				if (LineOfText == null) {
					Done = true;
				} else {
					if(LineOfText.length() >= 9) {
						int j = 0;
						char value;
						while(j < 9) {
							value = LineOfText.charAt(j);
	            			values[i][j] = value;//Integer.valueOf(value);
	            			//System.out.println("values[" + i + "][" + j + "], " + value);
							j++;						
						}
					} else {
						System.out.println("Wrong file format (length==" + LineOfText.length() + ")");
						values = null;
						Done = true;
					}
					i++;
					//LineOfText.toUpperCase();
					//System.out.println("FileReader:: read: " + LineOfText);
					//text += LineOfText + System.getProperty("line.separator");
				} // if
			} // while
			if(i > 9) {
				System.out.println("Wrong file format (i==" + i + ")");
				values = null;
			}
			//System.out.println("(i==" + i + ")");
			

			//InFile.read(buffer);
			//System.out.println("" + buffer);			

		} catch (IOException Error) {
			System.out.println("FileReader:: Unable to create file stream");
		} // try-catch
			
     
		// Close the input file
		try {
			InFile.close();
//			System.out.println("FileReader:: closing input file  \t"
//					+ inputFile.getAbsolutePath());
		} catch (IOException e) {
		}
					
		return values;
	} // run()

}
